package com.axx.android.letter.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.axx.android.R;
import com.axx.android.letter.model.Tool;
import com.axx.android.utils.ImageLoader;

import java.util.List;

/**
 * Created by Administrator on 2017/7/22 0022.
 */

public class ToolAdapter extends SectionedRecyclerViewAdapter {
    private List<Tool.ToollistBean> list;
    private Context context;

    private View.OnClickListener itemClick;

    public void setItemClick(View.OnClickListener itemClick) {
        this.itemClick = itemClick;
    }

    public ToolAdapter(List<Tool.ToollistBean> list, Context context) {
        this.list = list;
        this.context = context;
        list.get(1).setOpen(true);
    }

    @Override
    protected int getSectionCount() {
        return list.size();
    }

    @Override
    protected int getItemCountForSection(int section) {
        return list.get(section).isOpen() == true ? list.get(section).getTools().size() : 0;
    }

    @Override
    protected boolean hasFooterInSection(int section) {
        return false;
    }

    @Override
    protected RecyclerView.ViewHolder onCreateSectionHeaderViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.view_item_tool_header, parent, false);
        return new HeaderHolder(view);
    }

    @Override
    protected RecyclerView.ViewHolder onCreateSectionFooterViewHolder(ViewGroup parent, int viewType) {
        return null;
    }

    @Override
    protected RecyclerView.ViewHolder onCreateItemViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.view_item_tool_item, parent, false);
        return new ItemHolder(view);
    }

    @Override
    protected void onBindSectionHeaderViewHolder(RecyclerView.ViewHolder holder, final int section) {
        HeaderHolder headerHolder = (HeaderHolder) holder;
        headerHolder.mTitle.setText(list.get(section).getLevelName());
        if (list.get(section).isOpen()) {
            headerHolder.mOpen.setBackgroundResource(R.drawable.ic_tool_open);
        } else {
            headerHolder.mOpen.setBackgroundResource(R.drawable.ic_tool_close);
        }
        headerHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                list.get(section).setOpen(!list.get(section).isOpen());
                notifyDataSetChanged();
            }
        });
        headerHolder.mOpen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                list.get(section).setOpen(!list.get(section).isOpen());
                notifyDataSetChanged();
            }
        });
    }

    @Override
    protected void onBindSectionFooterViewHolder(RecyclerView.ViewHolder holder, int section) {

    }

    @Override
    protected void onBindItemViewHolder(RecyclerView.ViewHolder holder, final int section, final int position) {
        final ItemHolder itemHolder = (ItemHolder) holder;

        Tool.ToollistBean.ToolsBean toolsBean = list.get(section).getTools().get(position);
        ImageLoader.loadImage(toolsBean.getPic(), itemHolder.imageView, context);
        itemHolder.name.setText(toolsBean.getName());
        itemHolder.count.setText(toolsBean.getNum() == 0 ? "" : "x" + toolsBean.getNum());

        if (toolsBean.getId() == 8){
            itemHolder.price.setText("免费");
        }else{
            itemHolder.price.setText("￥" + toolsBean.getPrice());
        }

        itemHolder.itemView.setTag(toolsBean);
        itemHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                itemClick.onClick(itemHolder.itemView);
                notifyDataSetChanged();
            }
        });
    }

    public class ItemHolder extends RecyclerView.ViewHolder {
        private ImageView imageView;
        private TextView name;
        private TextView count;
        private TextView price;


        public ItemHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.iv_image);
            name = (TextView) itemView.findViewById(R.id.tv_name);
            count = (TextView) itemView.findViewById(R.id.tv_count);
            price = (TextView) itemView.findViewById(R.id.tv_price);
        }
    }

    public class HeaderHolder extends RecyclerView.ViewHolder {
        private TextView mTitle;
        private Button mOpen;

        public HeaderHolder(View itemView) {
            super(itemView);
            mTitle = (TextView) itemView.findViewById(R.id.tv_title);
            mOpen = (Button) itemView.findViewById(R.id.btn_open);
        }
    }
}
